//package com.wss.bronze.gateway.core;
//
//import com.wss.bronze.gateway.core.config.GatewayProperties;
//import io.netty.bootstrap.ServerBootstrap;
//import io.netty.channel.ChannelFuture;
//import io.netty.channel.ChannelInitializer;
//import io.netty.channel.ChannelOption;
//import io.netty.channel.EventLoopGroup;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioServerSocketChannel;
//import io.netty.handler.codec.http.HttpObjectAggregator;
//import io.netty.handler.codec.http.HttpServerCodec;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.context.event.ApplicationReadyEvent;
//import org.springframework.context.ApplicationListener;
//import org.springframework.stereotype.Component;
//
//import java.util.concurrent.atomic.AtomicBoolean;
//
///**
// * @author wss
// */
//@Slf4j
//@Component
//public class GatewayServerBck implements ApplicationListener<ApplicationReadyEvent> {
//
//    @Autowired
//    private GatewayProperties properties;
//
//    private EventLoopGroup bossGroup;
//    private EventLoopGroup workerGroup;
//
//    private final AtomicBoolean running = new AtomicBoolean(false);
//
//    @Override
//    public void onApplicationEvent(ApplicationReadyEvent event) {
//        start();
//    }
//
//    public void start() {
//        if (!running.compareAndSet(false, true)) {
//            log.info("Gateway server is already running");
//            return;
//        }
//
//        // 创建 GatewayServerHandler 实例
//        GatewayServerHandler gatewayServerHandler = new GatewayServerHandler();
//
//        bossGroup = new NioEventLoopGroup(1);
//        workerGroup = new NioEventLoopGroup();
//
//        try {
//            ServerBootstrap b = new ServerBootstrap();
//            b.group(bossGroup, workerGroup)
//             .channel(NioServerSocketChannel.class)
//             .childHandler(new ChannelInitializer<SocketChannel>() {
//                 @Override
//                 protected void initChannel(SocketChannel ch) {
//                     ch.pipeline()
//                             .addLast(new HttpServerCodec())
//                             .addLast(new HttpObjectAggregator(1024 * 1024))
//                             .addLast(gatewayServerHandler);
//                 }
//             })
//             .option(ChannelOption.SO_BACKLOG, 128)
//             .childOption(ChannelOption.SO_KEEPALIVE, true);
//
//            ChannelFuture f = b.bind(properties.getPort()).sync();
//            log.info("Gateway started on port: {}", properties.getPort());
//            f.channel().closeFuture().sync();
//
//        } catch (Exception e) {
//            log.error("Gateway server error", e);
//        } finally {
//            stop();
//        }
//    }
//
//    public void stop() {
//        if (bossGroup != null) {
//            bossGroup.shutdownGracefully();
//        }
//        if (workerGroup != null) {
//            workerGroup.shutdownGracefully();
//        }
//    }
//}
